﻿using MaterialDesignThemes.Wpf;
using project.B12.IService;
using project.B12.Model.Res;
using software.Common.MVVM;
using System;

namespace project.B12.application.ViewModels.Dialogs
{
    public class ResetPasswordViewModel : ObservableObject
    {
        #region 构造函数
        public ResetPasswordViewModel(IUserService userService)
        {
            UserService = userService;
        }
        #endregion

        #region Command命令
        public RelayCommand SavePasswordCommand => new RelayCommand(SavePassword);//保存密码
        public RelayCommand ExitViewCommand => new RelayCommand(ExitView);//退出窗体
        #endregion

        #region Model属性
        public IUserService UserService { get; set; }//用户服务入口
        private string userName;//用户名
        private string password;//密码
        private string passwordTips;//密码提示
        private string secondaryPassword;//二次密码
        private string secondaryPasswordTips;//二次密码提示
        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }
        public string Password
        {
            get { return password; }
            set { password = value; RaisePropertyChanged(); }
        }
        public string PasswordTips
        {
            get { return passwordTips; }
            set { passwordTips = value; RaisePropertyChanged(); }
        }
        public string SecondaryPassword
        {
            get { return secondaryPassword; }
            set { secondaryPassword = value; RaisePropertyChanged(); }
        }
        public string SecondaryPasswordTips
        {
            get { return secondaryPasswordTips; }
            set { secondaryPasswordTips = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="obj"></param>
        private void SavePassword()
        {
            System.Windows.Application.Current.Dispatcher.Invoke(() =>
            {
                try
                {
                    if (DialogHost.IsDialogOpen("Main"))
                    {
                        PasswordTips = string.Empty;
                        SecondaryPasswordTips = string.Empty;
                        if (string.IsNullOrEmpty(Password))
                        {
                            PasswordTips = Resources.PasswordEmpty;
                            return;
                        }
                        var result = UserService.CheckPassword(Password);
                        if (!result.Success)
                        {
                            PasswordTips = Resources.PasswordFormat;
                            return;
                        }
                        if (string.IsNullOrEmpty(SecondaryPassword))
                        {
                            SecondaryPasswordTips = Resources.PasswordEmpty;
                            return;
                        }
                        if (Password != SecondaryPassword)
                        {
                            SecondaryPasswordTips = Resources.PasswordDifferent;
                            return;
                        }
                        DialogHost.Close("Main", Password);//成功
                    }
                }
                catch (Exception ex)
                {
                    PasswordTips = ex.Message;
                }
            });
        }

        /// <summary>
        /// 退出窗体
        /// </summary>
        /// <param name="obj"></param>
        private void ExitView()
        {
            if (DialogHost.IsDialogOpen("Main"))
                DialogHost.Close("Main", null);
        }
        #endregion
    }
}
